package com.ruiaa.meeting.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiaa.meeting.common.BusinessException;
import com.ruiaa.meeting.common.CommonResponse;
import com.ruiaa.meeting.common.Constant;
import com.ruiaa.meeting.common.ResponseCode;
import com.ruiaa.meeting.model.Api;
import com.ruiaa.meeting.model.MeetingRoom;
import com.ruiaa.meeting.model.User;
import com.ruiaa.meeting.model.vo.UserVo;
import com.ruiaa.meeting.persistence.UserMapper;
import com.ruiaa.meeting.service.ApiService;
import com.ruiaa.meeting.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author chenhuaiyi
 * @date 2022-11-04 10:49
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApiService apiService;

    @Override
    public CommonResponse<UserVo> login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("password",password);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null){
            if(user.getState().equals(Constant.USER_STATE.DISABLE)){
                throw new BusinessException(ResponseCode.ERROR.getCode(), "用户处于禁用状态");
            }
            // 生成token，并将用户信息存入session中
            StpUtil.login(username);
            StpUtil.getSession().set(username, user);
            // 获取api信息
            List<Api> relationalApis = apiService.getRelationalApis(1);
            return CommonResponse.createForSuccessIncludeApis(userToUserVo(user), relationalApis);
        }
        throw new BusinessException(ResponseCode.ERROR.getCode(), "用户名或密码错误");
    }

    private UserVo userToUserVo(User user){
        UserVo userVo = new UserVo();
        userVo.setUsername(user.getUsername());
        userVo.setRole(user.getRole());
        userVo.setState(user.getState());
        return userVo;
    }

    @Override
    public CommonResponse<List<User>> getAllAdmin(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        List<User> userList = userMapper.selectList(queryWrapper);
        if (userList != null) {
            List<Api> relationalApis = apiService.getRelationalApis(2);
            return CommonResponse.createForSuccessIncludeApis(userList, relationalApis);
        }
        return CommonResponse.createForError("查询失败");
    }

    @Override
    public CommonResponse<Object> addAdmin(String username, String password){
        QueryWrapper <User> queryWrapper = new QueryWrapper<>();
        List<User> userList = userMapper.selectList(queryWrapper);

        for (User user1 : userList){
            if (Objects.equals(user1.getUsername(), username)){
                throw new BusinessException(ResponseCode.ERROR.getCode(), "该管理员已存在");
            }
        }
        User user=new User();

        user.setUsername(username);
        user.setPassword(password);
        user.setState(0);
        user.setRole("ADMIN");
        int rows=userMapper.insert(user);
        if (rows>0) {
            List<Api> relationalApis = apiService.getRelationalApis(3);
            return CommonResponse.createForSuccessIncludeApis("已添加该管理员", relationalApis);
        }

        return CommonResponse.createForError("添加管理员失败");
    }

    @Override
    public CommonResponse<Object> removeAdmin(String username) {
        QueryWrapper <User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {

            userMapper.delete(queryWrapper);
            return CommonResponse.createForSuccess("删除管理员成功");

        }
        return CommonResponse.createForError("管理员不存在，删除管理员失败");

    }
    @Override
    public CommonResponse<UserVo> updateAdmin(User user){
        QueryWrapper <User> queryWrapper = new QueryWrapper<>();
        String username= user.getUsername();
        queryWrapper.eq("username", username);
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 != null) {

            int rows = userMapper.updateById(user1);
            if (rows > 0) {
                return CommonResponse.createForSuccess(userToUserVo(user1));
            }

        } else throw new BusinessException(ResponseCode.ERROR.getCode(), "管理员不存在");
        return CommonResponse.createForError("更新管理员信息失败");
    }


    @Override
    public CommonResponse<UserVo> setAdminPassword(String username, String password){
        QueryWrapper <User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user1 = userMapper.selectOne(queryWrapper);

        if (user1 != null) {
            user1.setPassword(password);
            int rows = userMapper.updateById(user1);

            if (rows > 0) {
                return CommonResponse.createForSuccess(userToUserVo(user1));
            }

        } else throw new BusinessException(ResponseCode.ERROR.getCode(), "管理员不存在");
        return CommonResponse.createForError("更新密码失败");
    }
    @Override
    public CommonResponse<Object> disableAdmin(String username){
        QueryWrapper <User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user1 = userMapper.selectOne(queryWrapper);

        if (user1 != null) {
            user1.setState(1);
            int rows = userMapper.updateById(user1);

            if (rows > 0) {
                return CommonResponse.createForSuccess("已修改");
            }

        } else throw new BusinessException(ResponseCode.ERROR.getCode(), "管理员不存在");
        return CommonResponse.createForError("更新失败");
    }

    @Override
    public CommonResponse<Object> ableAdmin(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user1 = userMapper.selectOne(queryWrapper);

        if (user1 != null) {
            user1.setState(0);
            int rows = userMapper.updateById(user1);

            if (rows > 0) {
                return CommonResponse.createForSuccess("已修改");
            }

        } else throw new BusinessException(ResponseCode.ERROR.getCode(), "管理员不存在");
        return CommonResponse.createForError("更新失败");
    }
}
